<template>
  <view class="reminders-container" v-if="messages.length > 0">
    <view class="reminders-header">
      <text class="title">工作提醒</text>
      <text class="clear" @tap="clearMessages">清空</text>
    </view>
    
    <scroll-view 
      class="messages-list" 
      scroll-y
      :scroll-top="scrollTop"
      :scroll-with-animation="true"
      enhanced
      :show-scrollbar="true"
      :style="{ height: '400rpx' }"
      @scroll="onScroll"
    >
      <view class="messages-wrapper">
        <view 
          v-for="(message, index) in messages" 
          :key="index"
          :class="['message-bubble', `type-${message.type}`, getAgeClass(message.timestamp)]"
          :style="{
            transform: `scale(${1 - Math.min(index * 0.03, 0.1)})`,
            opacity: `${1 - Math.min(index * 0.1, 0.3)}`,
            zIndex: messages.length - index
          }"
        >
          <view class="message-icon" :class="message.type">
            <text class="icon">{{ getIcon(message.type) }}</text>
          </view>
          <view class="message-content">
            <text class="text">{{ message.message }}</text>
            <text class="time">{{ formatTime(message.timestamp) }}</text>
          </view>
          <view class="bubble-arrow" :class="getAgeClass(message.timestamp)"></view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  name: 'WorkReminders',
  
  props: {
    messages: {
      type: Array,
      default: () => []
    }
  },

  data() {
    return {
      scrollTop: 0
    }
  },

  methods: {
    getIcon(type) {
      const icons = {
        activity: '🏃‍♂️',
        health: '💪',
        achievement: '🎉'
      }
      return icons[type] || '📝'
    },

    getAgeClass(timestamp) {
      const now = Date.now()
      const age = now - timestamp
      
      if (age < 5 * 60 * 1000) return 'recent' // 5分钟内
      if (age < 30 * 60 * 1000) return 'medium' // 30分钟内
      return 'old' // 30分钟以上
    },

    formatTime(timestamp) {
      const date = new Date(timestamp)
      const hours = date.getHours().toString().padStart(2, '0')
      const minutes = date.getMinutes().toString().padStart(2, '0')
      return `${hours}:${minutes}`
    },

    clearMessages() {
      this.$emit('clear')
    },

    onScroll(e) {
      this.scrollTop = e.detail.scrollTop
    }
  }
}
</script>

<style lang="scss">
.reminders-container {
  background: white;
  border-radius: 20px;
  padding: 20px;
  margin-top: 16px;
  box-shadow: 0 4px 16px rgba(148, 163, 184, 0.1);
  display: flex;
  flex-direction: column;

  .reminders-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    flex-shrink: 0;
    padding: 0 16rpx;

    .title {
      font-size: 18px;
      color: #0f172a;
      font-weight: 500;
    }

    .clear {
      font-size: 14px;
      color: #64748b;
      padding: 4px 8px;
      opacity: 0.8;
    }
  }

  .messages-list {
    width: 100%;
    box-sizing: border-box;
  }

  .messages-wrapper {
    padding: 16rpx;
    padding-left: 32rpx;
    padding-bottom: 32rpx;

    .message-bubble {
      display: flex;
      gap: 12px;
      padding: 16rpx 24rpx;
      border-radius: 16px;
      transition: all 0.3s ease;
      margin-bottom: 24rpx;
      position: relative;
      box-shadow: 0 2px 8px rgba(148, 163, 184, 0.08);
      transform-origin: left center;
      background: white;
      will-change: transform;

      &:last-child {
        margin-bottom: 0;
      }

      &.recent {
        background: linear-gradient(135deg, #38bdf8 0%, #0ea5e9 100%);
        box-shadow: 0 4px 12px rgba(14, 165, 233, 0.15);
        .message-content {
          .text {
            color: white;
            font-size: 28rpx;
            font-weight: 500;
          }
          .time {
            color: rgba(255, 255, 255, 0.8);
            font-size: 24rpx;
          }
        }
        .message-icon {
          background: rgba(255, 255, 255, 0.2);
          transform: scale(1.1);
        }
      }

      &.medium {
        background: #f8fafc;
        border: 1px solid #e2e8f0;
        box-shadow: 0 2px 6px rgba(148, 163, 184, 0.05);
      }

      &.old {
        background: #f8fafc;
        border: 1px solid #f1f5f9;
        opacity: 0.8;
        box-shadow: none;
        .message-content {
          .text {
            color: #64748b;
            font-size: 26rpx;
          }
          .time {
            color: #94a3b8;
            font-size: 22rpx;
          }
        }
      }

      .message-icon {
        width: 36px;
        height: 36px;
        border-radius: 18px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 20px;
        flex-shrink: 0;
        transition: all 0.3s ease;

        &.activity {
          background: rgba(14, 165, 233, 0.1);
        }

        &.health {
          background: rgba(16, 185, 129, 0.1);
        }

        &.achievement {
          background: rgba(245, 158, 11, 0.1);
        }
      }

      .message-content {
        flex: 1;
        min-width: 0;

        .text {
          font-size: 27rpx;
          color: #1e293b;
          margin-bottom: 4px;
          display: block;
          transition: all 0.3s ease;
          line-height: 1.4;
        }

        .time {
          font-size: 23rpx;
          color: #64748b;
          transition: all 0.3s ease;
        }
      }

      .bubble-arrow {
        position: absolute;
        left: -6px;
        top: 16px;
        width: 12px;
        height: 12px;
        transform: rotate(45deg);
        border-radius: 2px;
        z-index: 0;

        &.recent {
          background: #0ea5e9;
        }

        &.medium {
          background: #f8fafc;
          border-left: 1px solid #e2e8f0;
          border-bottom: 1px solid #e2e8f0;
        }

        &.old {
          background: #f8fafc;
          border-left: 1px solid #f1f5f9;
          border-bottom: 1px solid #f1f5f9;
        }
      }
    }
  }
}
</style> 